<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Molecule Viewer Test</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
        }

        .test-container {
            padding: 20px;
        }

        .viewer {
            width: 500px;
            height: 400px;
            border: 2px solid #007bff;
        }
    </style>
</head>

<body>
    <div class="test-container">
        <h1>Molecule Viewer Test</h1>
        <p>Testing if the molecule viewer can render mock data...</p>
        <div id="viewer" class="viewer"></div>
        <div id="status">Loading...</div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.min.js"></script>

    <script>
        console.log('Testing Molecule Viewer with mock data...');

        // Mock structure data (same format as our fallback)
        const mockStructureData = {
            material_id: 'mp-149',
            lattice: {
                a: 5.43, b: 5.43, c: 5.43,
                alpha: 90, beta: 90, gamma: 90,
                volume: 160.0
            },
            sites: [
                { species: ['Si'], xyz: [0, 0, 0], label: 'Si' },
                { species: ['Si'], xyz: [0.5, 0.5, 0], label: 'Si' },
                { species: ['Si'], xyz: [0.5, 0, 0.5], label: 'Si' },
                { species: ['Si'], xyz: [0, 0.5, 0.5], label: 'Si' },
                { species: ['Si'], xyz: [0.25, 0.25, 0.25], label: 'Si' },
                { species: ['Si'], xyz: [0.75, 0.75, 0.25], label: 'Si' },
                { species: ['Si'], xyz: [0.75, 0.25, 0.75], label: 'Si' },
                { species: ['Si'], xyz: [0.25, 0.75, 0.75], label: 'Si' }
            ]
        };

        // Test the rendering logic
        function testMoleculeRendering() {
            const container = document.getElementById('viewer');
            const status = document.getElementById('status');

            if (!container) {
                status.textContent = 'Container not found';
                return;
            }

            // Check if Three.js is available
            if (typeof THREE === 'undefined') {
                status.textContent = 'Three.js not loaded';
                return;
            }

            try {
                // Create scene
                const scene = new THREE.Scene();
                scene.background = new THREE.Color(0x0a0a0a);

                // Create camera
                const camera = new THREE.PerspectiveCamera(
                    75,
                    container.clientWidth / container.clientHeight,
                    0.1,
                    1000
                );
                camera.position.z = 15;

                // Create renderer
                const renderer = new THREE.WebGLRenderer({ antialias: true });
                renderer.setSize(container.clientWidth, container.clientHeight);
                container.appendChild(renderer.domElement);

                // Add lights
                const ambientLight = new THREE.AmbientLight(0x404040, 0.6);
                scene.add(ambientLight);

                const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
                directionalLight.position.set(1, 1, 1);
                scene.add(directionalLight);

                // Render mock atoms
                const atomColors = {
                    'Si': 0x909090, // Gray for Silicon
                    'default': 0xFF69B4
                };

                const atomRadii = {
                    'Si': 1.70,
                    'default': 1.80
                };

                mockStructureData.sites.forEach((atom) => {
                    const elementSymbol = atom.label || 'Si';
                    const color = atomColors[elementSymbol] || atomColors['default'];
                    const radius = (atomRadii[elementSymbol] || atomRadii['default']) * 1.2;

                    const geometry = new THREE.SphereGeometry(radius, 32, 32);
                    const material = new THREE.MeshPhongMaterial({ color });
                    const sphere = new THREE.Mesh(geometry, material);

                    // Convert fractional coordinates to Cartesian
                    const x = (atom.xyz[0] - 0.5) * mockStructureData.lattice.a;
                    const y = (atom.xyz[1] - 0.5) * mockStructureData.lattice.b;
                    const z = (atom.xyz[2] - 0.5) * mockStructureData.lattice.c;

                    sphere.position.set(x, y, z);
                    scene.add(sphere);
                });

                // Add orbit controls
                const controls = new THREE.OrbitControls(camera, renderer.domElement);
                controls.enableDamping = true;
                controls.dampingFactor = 0.05;

                // Animation loop
                function animate() {
                    requestAnimationFrame(animate);
                    controls.update();
                    renderer.render(scene, camera);
                }

                animate();

                status.textContent = 'Molecule rendered successfully! Check the viewer above.';
                console.log('Molecule viewer test completed successfully');

            } catch (error) {
                status.textContent = 'Error: ' + error.message;
                console.error('Molecule viewer test failed:', error);
            }
        }

        // Run the test
        testMoleculeRendering();
    </script>
</body>

</html>